package com.xjw.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ViewResolver;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xjw.entity.UserInfo;
import com.xjw.viewresolver.JsonViewResolver;

/**      
 * 项目名称：spring-project  
 * 实现功能：  
 * 类名称：CrossDomainController   
 * 版权所有：
 * 创建人：徐纪伟 
 * E-mail: 289045706@qq.com
 * 创建时间：2016年12月12日 上午10:28:42   	
 * 修改人：   
 * 修改时间：   
 * 修改备注：   
 * @version    
 */
@EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class CrossDomainController {

    
    @RequestMapping("/getUser2")
    public String getUser2() throws JsonProcessingException{
    	UserInfo user = new UserInfo();
        user.setName("zhangsan");
    	String returnData = new ObjectMapper().writeValueAsString(user);
    	return "<script>"
    			+ "window.location ='http://localhost:8081/html/proxy.html#" + returnData + "';"
    			+ "</script>";
    }
    
    @RequestMapping("/getUser3")
    public String getUser3() throws JsonProcessingException{
    	UserInfo user = new UserInfo();
        user.setName("zhangsan");
    	String returnData = new ObjectMapper().writeValueAsString(user);
    	return "<script> window.name = '" + returnData + "';</script>";
    }
    
    @RequestMapping("/getUser4")
    public ModelAndView getUser4(){
    	ModelAndView view = new ModelAndView();
    	UserInfo user = new UserInfo();
        user.setName("zhangsan");
    	view.addObject("user", user);
    	return view;
    }
    
    @Bean
    public ViewResolver jsonViewResolver() {
        return new JsonViewResolver();
    }
    
    public static void main(String[] args) {
    	SpringApplication.run(CrossDomainController.class);
    }
}

